package com.moonLight.weblog.web.event.subscriber;

import com.moonLight.weblog.common.domain.dos.BlogSettingsDO;
import com.moonLight.weblog.common.domain.dos.CommentDO;
import com.moonLight.weblog.common.domain.mapper.CommentMapper;
import com.moonLight.weblog.common.enums.CommentStatusEnum;
import com.moonLight.weblog.common.mail.MailHelper;
import com.moonLight.weblog.web.event.PublishCommentEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.tools.OptionChecker;
import java.util.Objects;

@Component
@Slf4j
public class PublishCommentSubscriber implements ApplicationListener<PublishCommentEvent> {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private MailHelper mailHelper;


    @Override
    @Async("threadPoolTaskExecutor")
    public void onApplicationEvent(PublishCommentEvent event) {
        Long commentId = event.getCommentId();  //新增的评论ID
        CommentDO commentDO = commentMapper.selectById(commentId);
        // 评论的相关信息
        Long replyCommentId = commentDO.getReplyCommentId();
        String nickname = commentDO.getNickname();
        String content = commentDO.getContent();
        Integer status = commentDO.getStatus();

        BlogSettingsDO blogSettingsDO = event.getBlogSettingsDO();
        // 是否开启评论审核功能
        Boolean isCommentExamineOpen = blogSettingsDO.getIsCommentExamineOpen();
        // 是否开启敏感词过滤功能
        Boolean isSensiWordOpen = blogSettingsDO.getIsCommentSensiWordOpen();
        // 博客名称
        String blogName = blogSettingsDO.getName();
        // 博主邮箱
        String authorMail = blogSettingsDO.getMail();
        //博客访问地址
        String blogUrl = "http://121.41.168.73/#/";


        /**
         *  1. 一级评论则直接发送邮件通知博主 replyCommentId为null
         *  2. 二级评论需要邮件通知被回复的用户 (当博主开启审核机制的前提下)
         */
        if (Objects.isNull(replyCommentId) && Objects.nonNull(authorMail)){  // 一级评论处理逻辑
            //拿到评论的路由地址
            String routerUrl = commentDO.getRouterUrl();
            String title = String.format("%s收到了评论", blogName);
            //判断评论状态以及是否开相关功能
            if (isCommentExamineOpen && Objects.equals(status , CommentStatusEnum.WAIT_EXAMINE.getCode())){
                title = title + "【待审核】";
            }
            // 如果开启了敏感词过滤，并且当前评论状态为 "审核不通过"，标题后缀添加【系统已拦截】标识
            if (isSensiWordOpen && Objects.equals(status, CommentStatusEnum.EXAMINE_FAILED.getCode())) {
                title = title + "【系统已拦截】";
            }
            // 构建 HTML
            String html = String.format("<html><body>" +
                            "<h2>路由:</h2><p>%s</p>" +
                            "<h2>%s 评论了你:</h2><p>%s</p>" +
                            "<p><a href='%s%s' target='_blank'>查看详情</a></p>" +
                            "</body></html>",
                    routerUrl, nickname, commentDO.getContent(), blogUrl, routerUrl);
            //发送邮件
            mailHelper.sendMail(authorMail, title, html);
        }else if (Objects.nonNull(replyCommentId) && Objects.equals(status,CommentStatusEnum.NORMAL.getCode())){ //二级评论 且状态正常
            if (isCommentExamineOpen){ //开启了审核 则先通知博主 , 博主审核通过后 再通知被回复的用户

            }  else {
                // 通过ID拿到被回复的评论Id 查询相关信息
                CommentDO replyCommentDO = commentMapper.selectById(replyCommentId);
                String nickname1 = replyCommentDO.getNickname();
                String to = replyCommentDO.getMail();

                String routerUrl = replyCommentDO.getRouterUrl();
                String title = String.format("你在%s的评论收到了回复", blogName);

                // 构建 HTML
                String html = String.format("<html><body>" +
                                "<h2>你的评论:</h2><p>%s</p>" +
                                "<h2>%s 回复了你:</h2><p>%s</p>" +
                                "<p><a href='%s%s' target='_blank'>查看详情</a></p>" +
                                "</body></html>",
                        replyCommentDO.getContent(), nickname, content, blogUrl, routerUrl);
                // 发送邮件
                mailHelper.sendMail(to, title, html);

            }

        }

    }
}
